package com.yanqu.road.server.manger;

import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.enums.eUpdateScope;
import com.yanqu.road.entity.servercenter.HotPackVersion;
import com.yanqu.road.entity.servercenter.HotfixUpdateInfo;
import com.yanqu.road.entity.servercenter.HotfixVersion;
import com.yanqu.road.logic.bussiness.servercenter.ChannelBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HotPackVersionMgr extends TempMgr {

	private static final Logger logger = LogManager.getLogger(HotPackVersionMgr.class.getName());

	private static List<HotPackVersion> hotPackVersionList;
	private static Map<String, Map<String, Map<String, HotPackVersion>>> hotPackVersionMap;
	private static Map<String, Map<String, HotPackVersion>> recommendHotPackVersionMap;
	private static Map<String, Map<String, HotPackVersion>> whiteRecommendHotPackVersionMap;        //白名单推荐版本
	private static Map<String, Map<String, HotPackVersion>> forwardRecommendHotPackVersionMap;      //先遣服推荐版本

	//新逻辑
	private static Map<String, Map<String, Map<String, List<HotfixVersion>>>> hotfixVersionMap;
	private static Map<String, HotfixUpdateInfo> hotfixUpdateInfoMap;

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean reloadData() {
		hotPackVersionList = ChannelBussiness.getHotPackVersionList();
		initRecommendVersionMap();

		//新逻辑
		hotfixVersionMap = ChannelBussiness.getHotfixVersionMap();
		hotfixUpdateInfoMap = ChannelBussiness.getHotfixUpdateInfoMap();
		return true;
	}

	@Override
	public boolean reloadConfig() throws Exception {
		return true;
	}

	/**
	 * 初始化推荐版本
	 */
	private void initRecommendVersionMap(){
		Map<String, Map<String, Map<String, HotPackVersion>>> versionMap = new ConcurrentHashMap<>();
		Map<String, Map<String, HotPackVersion>> recommendMap = new ConcurrentHashMap<>();
		Map<String, Map<String, HotPackVersion>> whiteDataMap = new ConcurrentHashMap<>();
		Map<String, Map<String, HotPackVersion>> forwardDataMap = new ConcurrentHashMap<>();
		for(HotPackVersion hotPackVersion : hotPackVersionList){
			if(!versionMap.containsKey(hotPackVersion.getPackageMark())){
				versionMap.put(hotPackVersion.getPackageMark(), new ConcurrentHashMap<>());
			}
			Map<String, Map<String, HotPackVersion>> tempVersionMap = versionMap.get(hotPackVersion.getPackageMark());
			if(!tempVersionMap.containsKey(hotPackVersion.getPackageVersion())){
				tempVersionMap.put(hotPackVersion.getPackageVersion(), new ConcurrentHashMap<>());
			}
			tempVersionMap.get(hotPackVersion.getPackageVersion()).put(hotPackVersion.getHotPackVersion(), hotPackVersion);

			if(!hotPackVersion.isDelete()){
				//普通
				if(hotPackVersion.isRecommendVersion()){
					if(!recommendMap.containsKey(hotPackVersion.getPackageMark())){
						recommendMap.put(hotPackVersion.getPackageMark(), new ConcurrentHashMap<>());
					}
					recommendMap.get(hotPackVersion.getPackageMark()).put(hotPackVersion.getPackageVersion(), hotPackVersion);
				}
				//白名单
				if(hotPackVersion.isWhiteRecommendVersion()){
					if(!whiteDataMap.containsKey(hotPackVersion.getPackageMark())){
						whiteDataMap.put(hotPackVersion.getPackageMark(), new ConcurrentHashMap<>());
					}
					whiteDataMap.get(hotPackVersion.getPackageMark()).put(hotPackVersion.getPackageVersion(), hotPackVersion);
				}
				//先遣服
				if(hotPackVersion.isForwardRecommendVersion()){
					if(!forwardDataMap.containsKey(hotPackVersion.getPackageMark())){
						forwardDataMap.put(hotPackVersion.getPackageMark(), new ConcurrentHashMap<>());
					}
					forwardDataMap.get(hotPackVersion.getPackageMark()).put(hotPackVersion.getPackageVersion(), hotPackVersion);
				}
			}
		}
		hotPackVersionMap = versionMap;
		recommendHotPackVersionMap = recommendMap;
		whiteRecommendHotPackVersionMap = whiteDataMap;
		forwardRecommendHotPackVersionMap = forwardDataMap;
	}

	/**
	 * 获取热更包信息
	 * @param packageMark
	 * @param packageVersion
	 * @param hotPackVersion
	 * @return
	 */
	public static HotPackVersion getHotPackVersion(String packageMark, String packageVersion, String hotPackVersion){
		Map<String, Map<String, HotPackVersion>> dataMap = hotPackVersionMap.get(packageMark);
		if(null != dataMap){
			Map<String, HotPackVersion> tempDataMap = dataMap.get(packageVersion);
			if(null != tempDataMap) {
				return tempDataMap.get(hotPackVersion);
			}
		}
		return null;
	}

	/**
	 * 获取推荐更新包版本
	 * @param packageMark
	 * @param packageVersion
	 * @return
	 */
	public static HotPackVersion getRecommendVersion(String packageMark, String packageVersion, boolean isWhiteUser, int forwardServer){
		HotPackVersion recommendVersion = null;
		if(isWhiteUser){//白名单
			Map<String, HotPackVersion> dataMap = whiteRecommendHotPackVersionMap.get(packageMark);
			if(null != dataMap) {
				recommendVersion = dataMap.get(packageVersion);
			}
		}else {
			if(forwardServer > 0){//先遣服
				Map<String, HotPackVersion> dataMap = forwardRecommendHotPackVersionMap.get(packageMark);
				if (null != dataMap) {
					recommendVersion = dataMap.get(packageVersion);
				}
			}else {
				Map<String, HotPackVersion> dataMap = recommendHotPackVersionMap.get(packageMark);
				if (null != dataMap) {
					recommendVersion = dataMap.get(packageVersion);
				}
			}
		}
		return recommendVersion;
	}

	/**
	 * 获取热更包信息
	 * @param packageMark
	 * @param packageVersion
	 * @param hotPackVersion
	 * @return
	 */
	private static List<HotfixVersion> getHotfixVersionList(String packageMark, String packageVersion, String hotPackVersion){
		Map<String, Map<String, List<HotfixVersion>>> dataMap = hotfixVersionMap.get(packageMark);
		if(null != dataMap){
			Map<String, List<HotfixVersion>> tempDataMap = dataMap.get(packageVersion);
			if(null != tempDataMap) {
				return tempDataMap.get(hotPackVersion);
			}
		}
		return null;
	}

	/**
	 * 获取热更包信息
	 * @param packageMark
	 * @param packageVersion
	 * @param hotPackVersion
	 * @return
	 */
	public static HotfixVersion getHotfixVersion(String packageMark, String packageVersion, String hotPackVersion, boolean isWhiteUser, int forwardServer){
		List<HotfixVersion> dataList = getHotfixVersionList(packageMark, packageVersion, hotPackVersion);
		if(null == dataList){
			return null;
		}
		int updateScope;
		if(isWhiteUser){//白名单
			updateScope = eUpdateScope.WhiteUser.getValue();
			for(HotfixVersion hotfixVersion : dataList) {
				if (hotfixVersion.getUpdateScopeList().contains(updateScope)) {
					return hotfixVersion;
				}
			}
		}else if(forwardServer > 0){//先遣服
			updateScope = eUpdateScope.ForwardServer.getValue();
			for(HotfixVersion hotfixVersion : dataList) {
				if (hotfixVersion.getUpdateScopeList().contains(updateScope)) {
					return hotfixVersion;
				}
			}
		}else {
			updateScope = eUpdateScope.FormalServer.getValue();
			for (HotfixVersion hotfixVersion : dataList) {
				if (hotfixVersion.getUpdateScopeList().contains(updateScope)) {
					return hotfixVersion;
				}
			}
		}
		return null;
	}

	public static HotfixUpdateInfo getHotfixUpdateInfo(String packageMark){
		return hotfixUpdateInfoMap.get(packageMark);
	}

	public static boolean isHotfixVersionReview(HotfixVersion hotfixVersion, String appId, int channelId){
		if(null == hotfixVersion){
			return false;
		}
		boolean isReview = false;
		if(hotfixVersion.isReview()){
			if(!StringUtils.isNullOrEmpty(appId) && eChannelType.isMainLandChannel(channelId)){//大陆才有appId，
				if(hotfixVersion.getReviewAppIdList().size() > 0){
					if(hotfixVersion.getReviewAppIdList().contains(appId)){
						isReview = hotfixVersion.isReview();
					}else {
						isReview = false;
					}
				}else {
					isReview = hotfixVersion.isReview();
				}
			}else {
				isReview = hotfixVersion.isReview();
			}
		}else {
			isReview = hotfixVersion.isReview();
		}
		return isReview;
	}

	public static boolean isHotPackVersionReview(HotPackVersion hotPackVersion, String appId, int channelId){
		if(null == hotPackVersion){
			return false;
		}
		boolean isReview = false;
		if(hotPackVersion.isReview()){
			if(!StringUtils.isNullOrEmpty(appId) && eChannelType.isMainLandChannel(channelId)){//大陆才有appId，
				if(hotPackVersion.getReviewAppIdList().size() > 0){
					if(hotPackVersion.getReviewAppIdList().contains(appId)){
						isReview = hotPackVersion.isReview();
					}else {
						isReview = false;
					}
				}else {
					isReview = hotPackVersion.isReview();
				}
			}else {
				isReview = hotPackVersion.isReview();
			}
		}else {
			isReview = hotPackVersion.isReview();
		}
		return isReview;
	}
}
